package com.handmark.pulltorefresh.library;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor;
import com.handmark.pulltorefresh.library.internal.LoadingLayout;

public class PullToRefreshListView extends PullToRefreshAdapterViewBase<ListView> {
    public OverscrollHelper mOverscrollHelper;
    FrameLayout mLvFooterLoadingFrame;
    private LoadingLayout mHeaderLoadingView;
    private LoadingLayout mFooterLoadingView;
    private boolean mListViewExtrasEnabled;
    private Runnable k = new Runnable() {
        @Override
        public void run() {
            h();
        }
    };

    public PullToRefreshListView(Context arg2) {
        super(arg2);
    }

    public PullToRefreshListView(Context arg2, AttributeSet arg3) {
        super(arg2, arg3);
    }

    public PullToRefreshListView(Context arg2, Mode arg3) {
        super(arg2, arg3);
    }

    public PullToRefreshListView(Context arg2, Mode arg3, AnimationStyle arg4) {
        super(arg2, arg3, arg4);
    }

    public final ListView createRefreshableView(Context context, AttributeSet attributeSet) {
        ListView listView = this.createListView(context, attributeSet);
        listView.setId(android.R.id.list);//16908298
        if (Build.VERSION.SDK_INT < 21 || Build.VERSION.SDK_INT > 22) {
            if (android.support.design.a.y == null) {
                String v1 = Build.BRAND;
                android.support.design.a.y = !TextUtils.isEmpty(v1) ? Boolean.valueOf(v1.toLowerCase().contains("coolpad")) : Boolean.valueOf(false);
            }
            if (!android.support.design.a.y.booleanValue() && !android.support.design.a.r()) {
                return listView;
            }
            ViewCompat.setOverScrollMode(listView, ViewCompat.OVER_SCROLL_NEVER);
        } else {
            ViewCompat.setOverScrollMode(listView, ViewCompat.OVER_SCROLL_NEVER);
        }
        return listView;
    }

    @Override
    protected final LoadingLayoutProxy createLoadingLayoutProxy(boolean includeStart, boolean includeEnd) {
        LoadingLayoutProxy proxy = super.createLoadingLayoutProxy(includeStart, includeEnd);
        if (this.mListViewExtrasEnabled) {
            Mode mode = this.getMode();
            if ((includeStart) && (mode.showHeaderLoadingLayout())) {
                proxy.addLayout(this.mHeaderLoadingView);
            }
            if (!includeEnd) {
                return proxy;
            }
            if (!mode.showFooterLoadingLayout()) {
                return proxy;
            }
            proxy.addLayout(this.mFooterLoadingView);
        }
        return proxy;
    }

    @Override
    protected final void handleStyledAttributes(TypedArray a) {
        super.handleStyledAttributes(a);
        this.mListViewExtrasEnabled = a.getBoolean(com.ss.android.common.dd.PullToRefresh_ptrListViewExtrasEnabled, true);
        if (this.mListViewExtrasEnabled) {
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER_HORIZONTAL);
            FrameLayout frame = new FrameLayout(this.getContext());
            this.mHeaderLoadingView = this.createLoadingLayout(this.getContext(), Mode.PULL_FROM_START, a);
            this.mHeaderLoadingView.setVisibility(GONE);
            this.mHeaderLoadingView.a(1);
            frame.addView(this.mHeaderLoadingView, lp);
            this.mLoadingLayoutList.add(this.mHeaderLoadingView);
            this.mRefreshableView.addHeaderView(frame, null, false);
            this.mLvFooterLoadingFrame = new FrameLayout(this.getContext());
            this.mFooterLoadingView = this.createLoadingLayout(this.getContext(), Mode.PULL_FROM_END, a);
            this.mFooterLoadingView.setVisibility(GONE);
            this.mLvFooterLoadingFrame.addView(this.mFooterLoadingView, lp);
            if (!a.hasValue(com.ss.android.common.dd.PullToRefresh_ptrScrollingWhileRefreshingEnabled)) {
                this.setScrollingWhileRefreshingEnabled(true);
            }
        }
    }

    public ListView createListView(Context context, AttributeSet attributeSet) {
        return new InternalListViewSDK9(context, attributeSet);
    }

    protected final void onRefreshing(boolean arg8, boolean doScroll) {
        int v1_1;
        LoadingLayout v5;
        int v0_1;
        LoadingLayout v3;
        LoadingLayout v4;
        ListAdapter v0 = this.mRefreshableView.getAdapter();
        if (!this.mListViewExtrasEnabled || !this.getShowViewWhileRefreshing() || v0 == null) {
            super.onRefreshing(arg8, doScroll);
        } else {
            super.onRefreshing(arg8, false);
            switch (getCurrentMode()) {
                case MANUAL_REFRESH_ONLY:
                case PULL_FROM_END: {
                    v5 = this.getFooterLayout();
                    v4 = this.mFooterLoadingView;
                    v3 = this.mHeaderLoadingView;
                    v1_1 = this.mRefreshableView.getCount() - 1;
                    v0_1 = this.getScrollY() - this.getFooterSize();
                    break;
                }
                default: {
                    v4 = this.getHeaderLayout();
                    v3 = this.mHeaderLoadingView;
                    LoadingLayout v1 = this.mFooterLoadingView;
                    v0_1 = this.getScrollY() + this.getHeaderSize();
                    v5 = v4;
                    v4 = v3;
                    v3 = v1;
                    v1_1 = 0;
                    break;
                }
            }

            v5.reset();
            v5.e();
            v3.setVisibility(GONE);
            v4.setVisibility(VISIBLE);
            v4.refreshing();
            if (!doScroll) {
                return;
            }

            this.mLayoutVisibilityChangesEnabled = false;
            this.setHeaderScroll(v0_1);
            this.mRefreshableView.setSelection(v1_1);
            this.smoothScrollTo(0);
        }
    }

    public final void onRefreshComplete() {
        if (this.c()) {
            this.removeCallbacks(this.k);
            this.post(this.k);
        }
    }

    protected final void onReset() {
        LoadingLayout v3_1;
        int v0;
        int v3;
        LoadingLayout v4;
        LoadingLayout v5;
        int v2 = 0;
        int v1 = 1;
        if (!this.mListViewExtrasEnabled) {
            super.onReset();
        } else {
            switch (getCurrentMode()) {
                case MANUAL_REFRESH_ONLY:
                case PULL_FROM_END: {
                    LoadingLayout v6 = this.getFooterLayout();
                    v5 = this.mFooterLoadingView;
                    v3 = this.mRefreshableView.getCount() - 1;
                    int v4_1 = this.getFooterSize();
                    v0 = Math.abs(this.mRefreshableView.getLastVisiblePosition() - v3) <= 1 ? 1 : 0;
                    v2 = v3;
                    v1 = v4_1;
                    v3_1 = v5;
                    v4 = v6;
                    break;
                }
                default: {
                    v5 = this.getHeaderLayout();
                    v4 = this.mHeaderLoadingView;
                    v3 = -this.getHeaderSize();
                    if (Math.abs(this.mRefreshableView.getFirstVisiblePosition()) > 1) {
                        v1 = 0;
                    }

                    v0 = v1;
                    v1 = v3;
                    v3_1 = v4;
                    v4 = v5;
                    break;
                }
            }

            if (v3_1.getVisibility() == VISIBLE) {
                v4.j();
                v3_1.setVisibility(GONE);
                if (v0 != 0 && this.getState() != State.MANUAL_REFRESHING) {
                    this.mRefreshableView.setSelection(v2);
                    this.setHeaderScroll(v1);
                }
            }

            super.onReset();
        }
    }

    public final Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    final void h() {
        super.onRefreshComplete();
    }

    public void setExtraEnabled(boolean extraEnabled) {
        super.setExtraEnabled(extraEnabled);
        if (this.mHeaderLoadingView != null) {
            this.mHeaderLoadingView.setExtraEnabled(extraEnabled);
        }
    }

    public void setOverScrollListener(OverscrollHelper overscrollHelper) {
        this.mOverscrollHelper = overscrollHelper;
    }

    public class InternalListView extends ListView implements EmptyViewMethodAccessor {
        private boolean mAddedLvFooter = false;

        public InternalListView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        protected void dispatchDraw(Canvas canvas) {
            try {
                super.dispatchDraw(canvas);
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }

        public boolean dispatchTouchEvent(MotionEvent motionEvent) {
            try {
                return super.dispatchTouchEvent(motionEvent);
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
                return false;
            }
        }

        protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY,
                                       int scrollRangeX, int scrollRangeY,
                                       int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
            if (mOverscrollHelper != null) {
                mOverscrollHelper.overScrollBy(deltaY, scrollY);
            }
            return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY,
                    maxOverScrollX, maxOverScrollY, isTouchEvent);
        }

        public void setAdapter(ListAdapter adapter) {
            if (mLvFooterLoadingFrame != null && !this.mAddedLvFooter) {
                this.addFooterView(mLvFooterLoadingFrame, null, false);
                this.mAddedLvFooter = true;
            }

            super.setAdapter(adapter);
        }

        public void setEmptyView(View emptyView) {
            PullToRefreshListView.this.setEmptyView(emptyView);
        }

        public void setEmptyViewInternal(View emptyView) {
            super.setEmptyView(emptyView);
        }
    }

    @TargetApi(value = 9)
    final class InternalListViewSDK9 extends InternalListView {

        public InternalListViewSDK9(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        protected final boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
                                             int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
            boolean returnValue = super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
                    scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
            OverscrollHelper.overScrollBy(PullToRefreshListView.this, deltaX, scrollX, deltaY, scrollY, 0, 0, 1f, isTouchEvent);
            return returnValue;
        }
    }
}

